var express = require('express');
var router = express.Router();

var mysql = require('../config/mysql');// 获取数据库连接配置

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', { title: '主页', user:req.session.user});
});
router.get('/index', function(req, res, next) {
    res.render('index', { title: '主页', user:req.session.user});
});



router.get('/sql', function (req, res, next){

    var sql='select * from t_user where id=?';
    mysql.db.getConnection(function(err, connection) {
        if (err) {
            console.log('getConnection');
            res.render('index', { title: '数据库连接失败首页sql'+sql });
            return;
        }
        connection.query(sql, [1] , function selectTable(err, rows, fields){
            if (err){
                throw err;
            }
            if (rows){
                for(var i = 0 ; i < rows.length ; i++){
                    console.log("%d\t%s\t%s", rows[i].id,rows[i].userName,rows[i].passWord);
                    sql=rows[i].id+' '+rows[i].userName+' '+rows[i].passWord;
                }
                res.render('index', { title: sql });
            }
        });
    });

});



module.exports = router;
